//
//  E7AnimationRoot.cpp
//  EndlessBattle
//
//  Created by cc on 12-4-23.
//  Copyright (c) 2012年 PalmGame. All rights reserved.
//

#include "E7AnimationRoot.h"

CE7AnimationRoot::CE7AnimationRoot() :
m_eID(ANI_NONE),
m_bIsLoop(false),
m_nFrameMax(0),
m_pFrames(NULL),
m_fInterval(1.0f),
m_pSheet(NULL),
m_strFile(0),
m_bIsPlaying(false),
m_nFrameIndex(0)
{
    m_pRoot = new CCNode() ;
    addChild(m_pRoot) ;
}

CE7AnimationRoot::~CE7AnimationRoot()
{
    SAFE_DELETE(m_pFrames) ;
    SAFE_DELETE(m_pRoot) ;
}

void CE7AnimationRoot::step(ccTime dt)
{
//    printf("enter CE7AnimationRoot::step --------------------------------------- \n") ;
    if (!m_bIsPlaying)
    {
        return ;
    }

    if (m_nFrameIndex >= m_nFrameMax)
    {
        if (!m_bIsLoop)
        {
            m_bIsPlaying = false ;
            return ;
        }
        m_nFrameIndex = 0 ;
    }

    for (int i = 0; i < m_nFrameMax; ++i)
    {
        m_pFrames[i].Hide() ;
    }
    m_pFrames[m_nFrameIndex].Show() ;
    ++m_nFrameIndex ;

//    printf("exit CE7AnimationRoot::step\n") ;
}

void CE7AnimationRoot::Start()
{
    schedule(schedule_selector(CE7AnimationRoot::step), m_fInterval) ;
    m_bIsPlaying = true ;
    m_nFrameIndex = 0 ;
    if (NULL != m_pSheet)
    {
        m_pRoot->addChild(m_pSheet) ;
    }
}

void CE7AnimationRoot::Stop()
{
    unscheduleAllSelectors() ;
    m_bIsPlaying = false ;
    m_nFrameIndex = 0 ;
    m_pRoot->removeAllChildrenWithCleanup(false) ;
}










